﻿
using AuthorizationServicesTest.AuthorizationServices;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AuthorizationServicesTest
{
    /// <summary>
    /// Summary description for AuthorizationServices
    /// </summary>
    [TestClass]
    public class AuthorizationServiceUnitTest
    {
        public const string ENDPOINTNAME = "webHttpBindingAjax"; //"netNamedPipeBinding";
        public const string APPLICATIONNAME1 = "InterfaceUnica";
        public const string APPLICATIONNAME2 = "AuthServicesUnitTest2";
        public const string OPERATION_READ = "ReadRecord";
        public const string OPERATION_DELETE = "DeleteRecord";
        public const string OPERATION_SAVE = "SaveRecord";
        public const string MANAGE_TASK = "ManageRecord";
        public const string READONLY_TASK = "ReadOnly";
        public const string CONTRIBUTE_TASK = "Contribute";
        public const string ROLE_MANAGER = "Administrator";
        public const string ROLE_READER = "Reader";
        public const string ROLE_CONTRIBUTOR = "Contributor";
        public const string SCOPEAPP1_1 = "Scope1";
        public const string SCOPEAPP1_2 = "Scope2";
        public const string SCOPEAPP2 = "Scope3";
        public const string USERNAME1 = "alan";//"spsadmingit";
        public const string USERNAME2 = "spsadmin";

        public AuthorizationServiceUnitTest()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        #region Tests for Operations
        [TestMethod]
        [Description("Test if service gets all operations for a application specified")]
        public void GetAllOperations()
        {
            var client = new AuthorizationServiceClient(ENDPOINTNAME);

            try
            {
                Operation[] operations = client.GetAllOperations(APPLICATIONNAME1);

                CollectionAssert.AllItemsAreNotNull(operations);

                Assert.IsFalse(operations.Length == 0);

                client.Close();
            }
            catch (System.Exception ex)
            {
                client.Abort();

                Assert.Fail(ex.ToString());
            }

        }

        [TestMethod]
        [Description("Test if service gets a specified operation")]
        public void GetOperation()
        {
            var client = new AuthorizationServiceClient(ENDPOINTNAME);

            try
            {
                Operation operation = client.GetOperation(APPLICATIONNAME1, OPERATION_READ);

                Assert.IsNotNull(operation);

                Assert.IsTrue(operation.Name == OPERATION_READ);

                Assert.IsTrue(operation.operationId > 0);

                client.Close();
            }
            catch (System.Exception ex)
            {
                client.Abort();

                Assert.Fail(ex.ToString());
            }

        }

        [TestMethod]
        [Description("Test if service creates specified operation")]
        public void CreateOperation()
        {
            var client = new AuthorizationServiceClient(ENDPOINTNAME);

            try
            {
                Operation tempoperation = new Operation();

                tempoperation.operationId = 10;

                tempoperation.Name = "TESTOPERATION";

                tempoperation.Description = "DESCRIPTION TEST";

                if (client.OperationExists(APPLICATIONNAME1, tempoperation.Name))
                    client.DeleteOperation(APPLICATIONNAME1,tempoperation.Name) ;

                Operation operation = client.CreateOperation(APPLICATIONNAME1, tempoperation.Name, tempoperation.Description, tempoperation.operationId);

                Assert.IsTrue(tempoperation.operationId == operation.operationId);

                Assert.IsTrue(tempoperation.Description == operation.Description);

                Assert.IsTrue(tempoperation.Name == operation.Name);

                client.DeleteOperation(APPLICATIONNAME1, tempoperation.Name);

                client.Close();
            }
            catch (System.Exception ex)
            {
                client.Abort();

                Assert.Fail(ex.ToString());
            }

        }

        [TestMethod]
        [Description("Test if a operation exists")]
        public void OperationExists()
        {
            var client = new AuthorizationServiceClient(ENDPOINTNAME);

            try
            {

                Assert.IsTrue(client.OperationExists(APPLICATIONNAME1, OPERATION_READ));

                Assert.IsFalse(client.OperationExists(APPLICATIONNAME1, "OPTHATNOTEXISTS"));

                client.Close();
            }
            catch (System.Exception ex)
            {
                client.Abort();

                Assert.Fail(ex.ToString());
            }

        }

        [TestMethod]
        [Description("Test the service that deletes an existing operation")]
        public void DeleteOperation()
        {
            var client = new AuthorizationServiceClient(ENDPOINTNAME);

            try
            {
                Operation tempoperation = new Operation();

                tempoperation.operationId = 10;

                tempoperation.Name = "TESTOPERATION";
                Operation operation;

                tempoperation.Description = "DESCRIPTION TEST";
                
                if (!client.OperationExists(APPLICATIONNAME1, tempoperation.Name))
                  operation = client.CreateOperation(APPLICATIONNAME1, tempoperation.Name, tempoperation.Description, tempoperation.operationId);
                else
                {
                    operation = client.GetOperation(APPLICATIONNAME1, tempoperation.Name);   
                   
                }
                client.DeleteOperation(APPLICATIONNAME1, tempoperation.Name);

                Assert.IsFalse(client.OperationExists(APPLICATIONNAME1, tempoperation.Name));

                client.Close();
            }
            catch (System.Exception ex)
            {
                client.Abort();

                Assert.Fail(ex.ToString());
            }

        }

        [TestMethod]
        [Description("Test the service that updates an existing operation")]
        public void UpdateOperation()
        {
            var client = new AuthorizationServiceClient(ENDPOINTNAME);

            try
            {

                string upopname = "UPDATEDOPNAME";

                string upopdesc = "UPDATEDOPDESC";

                int upopid = 11;

                Operation tempoperation = new Operation();

                tempoperation.operationId = 10;

                tempoperation.Name = "TESTOPERATIONUPDATE";

                tempoperation.Description = "DESCRIPTION TEST";

                if (!client.OperationExists(APPLICATIONNAME1, tempoperation.Name))
                     client.CreateOperation(APPLICATIONNAME1, tempoperation.Name, tempoperation.Description, tempoperation.operationId);


                if (client.OperationExists(APPLICATIONNAME1, upopname))
                    client.DeleteOperation(APPLICATIONNAME1,upopname);

                client.UpdateOperation(APPLICATIONNAME1, tempoperation.Name, upopname, upopdesc, upopid);

                Assert.IsFalse(client.OperationExists(APPLICATIONNAME1, tempoperation.Name));

                Operation operation = client.GetOperation(APPLICATIONNAME1, upopname);

                Assert.IsTrue(operation.operationId == upopid);

                Assert.IsTrue(operation.Description == upopdesc);

                Assert.IsTrue(operation.Name == upopname);

                client.DeleteOperation(APPLICATIONNAME1, upopname);

                client.Close();
            }
            catch (System.Exception ex)
            {
                client.Abort();

                Assert.Fail(ex.ToString());
            }

        }

        #endregion

        #region Tests for Tasks
        [TestMethod]
        [Description("Test if service gets all Tasks for a application specified")]
        public void GetAllTasks()
        {
            var client = new AuthorizationServiceClient(ENDPOINTNAME);

            try
            {
                Task[] tasks = client.GetAllTasks(APPLICATIONNAME1, null);

                CollectionAssert.AllItemsAreNotNull(tasks);

                Assert.IsFalse(tasks.Length == 0);

                client.Close();
            }
            catch (System.Exception ex)
            {
                client.Abort();

                Assert.Fail(ex.ToString());
            }

        }

        [TestMethod]
        [Description("Test if service gets all Task Names for a application specified")]
        public void GetAllTaskNames()
        {
            var client = new AuthorizationServiceClient(ENDPOINTNAME);

            try
            {
                string[] tasks = client.GetAllTaskNames(APPLICATIONNAME1, null);

                CollectionAssert.AllItemsAreNotNull(tasks);

                Assert.IsFalse(tasks.Length == 0);

                for (int i = 0; i <= tasks.Length; i++)
                {
                    Assert.AreNotEqual(tasks, string.Empty);
                }

                client.Close();
            }
            catch (System.Exception ex)
            {
                client.Abort();

                Assert.Fail(ex.ToString());
            }

        }

        [TestMethod]
        [Description("Test if service gets a specified task")]
        public void GetTask()
        {
            var client = new AuthorizationServiceClient(ENDPOINTNAME);

            try
            {
                Task task = client.GetTask(APPLICATIONNAME1, null, MANAGE_TASK);

                Assert.IsNotNull(task);

                Assert.IsTrue(task.Name == MANAGE_TASK);

                Assert.IsNotNull(task.Definition);

                client.Close();
            }
            catch (System.Exception ex)
            {
                client.Abort();

                Assert.Fail(ex.ToString());
            }

        }





        [TestMethod]
        [Description("Test if service creates specified task")]
        public void CreateTask()
        {
            var client = new AuthorizationServiceClient(ENDPOINTNAME);

            try
            {
                var temptask = new Task();

                temptask.Description = "TESTTASKDESC";

                temptask.Name = "TESTTASKNAME";
                if (client.TaskExists(APPLICATIONNAME1, null, temptask.Name))
                    client.DeleteTask(APPLICATIONNAME1, null, temptask.Name);


                Task task = client.CreateTask(APPLICATIONNAME1, null, temptask.Name, temptask.Description);

                Assert.IsTrue(task.Name == temptask.Name);

                Assert.IsTrue(task.Description == temptask.Description);

                client.DeleteTask(APPLICATIONNAME1, null, task.Name);

                client.Close();
            }
            catch (System.Exception ex)
            {
                client.Abort();

                Assert.Fail(ex.ToString());
            }

        }

        [TestMethod]
        [Description("Test if service deletes a specified task")]
        public void DeleteTask()
        {
            var client = new AuthorizationServiceClient(ENDPOINTNAME);

            try
            {
                Task temptask = new Task();

                temptask.Description = "TESTTASKDESC";

                temptask.Name = "TESTTASKNAME";

                if (!client.TaskExists(APPLICATIONNAME1, null, temptask.Name))
                     client.CreateTask(APPLICATIONNAME1, null, temptask.Name, temptask.Description);

                client.DeleteTask(APPLICATIONNAME1, null, temptask.Name);

                Assert.IsFalse(client.TaskExists(APPLICATIONNAME1,null, temptask.Name));

                client.Close();
            }
            catch (System.Exception ex)
            {
                client.Abort();

                Assert.Fail(ex.ToString());
            }

        }

        [TestMethod]
        [Description("Test the service that updates an existing task")]
        public void UpdateTask()
        {
            var client = new AuthorizationServiceClient(ENDPOINTNAME);

            try
            {

                string uptskname = "UPDATEDOPNAME";

                string uptskdesc = "UPDATEDOPDESC";

                Task temptask = new Task();

                temptask.Name = "TESTTASK";

                temptask.Description = "DESCRIPTION TASK TEST";
     

                if (!client.TaskExists(APPLICATIONNAME1, null, temptask.Name))
                    client.CreateTask(APPLICATIONNAME1, null, temptask.Name, temptask.Description);

                client.UpdateTask(APPLICATIONNAME1, null, temptask.Name, uptskname, uptskdesc);

                Assert.IsFalse(client.TaskExists(APPLICATIONNAME1, null, temptask.Name));

                Task task = client.GetTask(APPLICATIONNAME1, null, uptskname);

                Assert.IsTrue(task.Description == uptskdesc);

                Assert.IsTrue(task.Name == uptskname);

                client.DeleteTask(APPLICATIONNAME1, null, uptskname);

                client.Close();
            }
            catch (System.Exception ex)
            {
                client.Abort();

                Assert.Fail(ex.ToString());
            }

        }

        [TestMethod]
        [Description("Test if a task exists")]
        public void TaskExists()
        {
            var client = new AuthorizationServiceClient(ENDPOINTNAME);

            try
            {

                Assert.IsTrue(client.TaskExists(APPLICATIONNAME1, null, MANAGE_TASK));

                Assert.IsFalse(client.TaskExists(APPLICATIONNAME1,null, "TSKTHATNOTEXISTS"));

                client.Close();
            }
            catch (System.Exception ex)
            {
                client.Abort();

                Assert.Fail(ex.ToString());
            }

        }


        #endregion

        #region Tests for Roles
        [TestMethod]
        [Description("Test if service gets all roles for a application specified")]
        public void GetAllRolesAssignments()
        {
            var client = new AuthorizationServiceClient(ENDPOINTNAME);

            try
            {
                const string roleName = "GetAllRolesAssignmentsTest";
                if (!client.RoleAssignmentExists(APPLICATIONNAME1, null, roleName))
                    client.CreateRoleAssignment(APPLICATIONNAME1, null, roleName, null);
                var role = client.GetAllRoleAssignments(APPLICATIONNAME1, null);

                CollectionAssert.AllItemsAreNotNull(role);

                Assert.IsFalse(role.Length == 0);
                client.DeleteRoleAssignment(APPLICATIONNAME1, null, roleName);
                client.Close();
            }
            catch (System.Exception ex)
            {
                client.Abort();

                Assert.Fail(ex.ToString());
            }

        }

        [TestMethod]
        [Description("Test if service gets all Role Names for a application specified")]
        public void GetAllRoleAssignmentNames()
        {
            var client = new AuthorizationServiceClient(ENDPOINTNAME);

            try
            {
                const string roleName = "GetAllRoleAssignmentNamesTest";
                if (!client.RoleAssignmentExists(APPLICATIONNAME1, null, roleName))
                    client.CreateRoleAssignment(APPLICATIONNAME1, null, roleName, null);

                string[] roles = client.GetAllRoleAssignmentNames(APPLICATIONNAME1, null);

                CollectionAssert.AllItemsAreNotNull(roles);

                Assert.IsFalse(roles.Length == 0);

                for (int i = 0; i <= roles.Length; i++)
                {
                    Assert.AreNotEqual(roles, string.Empty);
                }

                client.DeleteRoleAssignment(APPLICATIONNAME1, null, roleName);
                
                client.Close();
            }
            catch (System.Exception ex)
            {
                client.Abort();

                Assert.Fail(ex.ToString());
            }

        }


        [TestMethod]
        [Description("Test if service gets a specified role")]
        public void GetRoleAssignment()
        {
            var client = new AuthorizationServiceClient(ENDPOINTNAME);

            try
            {
                var role = client.GetRoleAssignment(APPLICATIONNAME1, null, ROLE_MANAGER);

                if (client.RoleAssignmentExists(APPLICATIONNAME1, null, ROLE_MANAGER))
                {
                    Assert.IsNotNull(role);


                    Assert.IsTrue(role.Name == ROLE_MANAGER);

                    Assert.IsTrue(role.RoleDefinitionNames.Length > 0);
                }
                //Assert.IsTrue(role.TaskNames.Length > 0);

                client.Close();
            }
            catch (System.Exception ex)
            {
                client.Abort();

                Assert.Fail(ex.ToString());
            }

        }

        [TestMethod]
        [Description("Test if service creates specified RoleAssignment")]
        public void CreateRoleAssignment()
        {
            var client = new AuthorizationServiceClient(ENDPOINTNAME);

            try
            {
                var rolenew = new RoleAssignment(); {
                    rolenew.Name = "teste";
                    rolenew.Description = "unit test";
                }

               if  (client.RoleAssignmentExists(APPLICATIONNAME1, null, rolenew.Name))
                   client.DeleteRoleAssignment(APPLICATIONNAME1, null, rolenew.Name);

                var role = client.CreateRoleAssignment(APPLICATIONNAME1, null, rolenew.Name, rolenew.Description);


                Assert.IsTrue(role.Description == rolenew.Description);

                Assert.IsTrue(role.Name == rolenew.Name);

                client.DeleteRoleAssignment(APPLICATIONNAME1, null, rolenew.Name);

                client.Close();
            }
            catch (System.Exception ex)
            {
                client.Abort();

                Assert.Fail(ex.ToString());
            }

        }

        [TestMethod]
        [Description("Test if a role exists")]
        public void RoleAssignmentExists()
        {
            var client = new AuthorizationServiceClient(ENDPOINTNAME);

            try
            {
                const string roleName = "ROLEASSIGNMENTEXISTSTEST";

                if (!client.RoleAssignmentExists(APPLICATIONNAME1, null, roleName))
                  client.CreateRoleAssignment(APPLICATIONNAME1, null, roleName, null);

                Assert.IsTrue(client.RoleAssignmentExists(APPLICATIONNAME1, null, roleName));

                Assert.IsFalse(client.RoleAssignmentExists(APPLICATIONNAME1, null, "ROLETHATNOTEXISTS"));

             
                client.DeleteRoleAssignment(APPLICATIONNAME1, null, roleName);
                client.Close();
            }
            catch (System.Exception ex)
            {
                client.Abort();

                Assert.Fail(ex.ToString());
            }

        }

        [TestMethod]
        [Description("Test the service that deletes an existing Role")]
        public void DeleteRoleAssignment()
        {
            var client = new AuthorizationServiceClient(ENDPOINTNAME);

            try
            {
                var temprole = new RoleAssignment();

                temprole.Name = "Test";

                temprole.Description = "Boss Description";
                RoleAssignment role;
                if (!client.RoleAssignmentExists(APPLICATIONNAME1, null, temprole.Name))
                {
                    role = client.CreateRoleAssignment(APPLICATIONNAME1, null, temprole.Name, temprole.Description);
                }
                else
                {
                    role = client.GetRoleAssignment(APPLICATIONNAME1, null, temprole.Name);
                }

                client.DeleteRoleAssignment(APPLICATIONNAME1, null, role.Name);

                Assert.IsFalse(client.RoleAssignmentExists(APPLICATIONNAME1, null, role.Name));

                client.Close();
            }
            catch (System.Exception ex)
            {
                client.Abort();

                Assert.Fail(ex.ToString());
            }

        }

        [TestMethod]
        [Description("Test the service that updates an existing roleassignment")]
        public void UpdateRoleAssignment()
        {
            var client = new AuthorizationServiceClient(ENDPOINTNAME);

            try
            {

                const string updatedrolename = "UPDATEDROLENAME";

                const string updatedroledesc = "UPDATEDROLEDESC";

                int upopid = 11;

                var tempoperation = new RoleAssignment {Name = "TESTROLEASSIGNMENT", Description = "DESCRIPTION TEST"};

                if (!client.RoleAssignmentExists( APPLICATIONNAME1, null, tempoperation.Name))
                     client.CreateRoleAssignment(APPLICATIONNAME1, null, tempoperation.Name, tempoperation.Description);

                if (client.RoleAssignmentExists(APPLICATIONNAME1, null, updatedrolename))
                    client.DeleteRoleAssignment(APPLICATIONNAME1, null, updatedrolename);

                    client.UpdateRoleAssignment(APPLICATIONNAME1, null, tempoperation.Name, updatedrolename, updatedroledesc);

                Assert.IsFalse(client.RoleAssignmentExists(APPLICATIONNAME1,null, tempoperation.Name));

                var roleAssignment = client.GetRoleAssignment(APPLICATIONNAME1, null, updatedrolename);


                Assert.IsTrue(roleAssignment.Description == updatedroledesc);

                Assert.IsTrue(roleAssignment.Name == updatedrolename);

                client.DeleteRoleAssignment(APPLICATIONNAME1, null, updatedrolename);

                client.Close();
            }
            catch (System.Exception ex)
            {
                client.Abort();

                Assert.Fail(ex.ToString());
            }

        }

        #endregion

        #region Tests for Applications

        [TestMethod]
        [Description("Test if service get all Application Names")]
        public void GetAllApplicationNames()
        {
            var client = new AuthorizationServiceClient(ENDPOINTNAME);

            try
            {
                string[] applications = client.GetAllApplicationNames();

                for (int i = 0; i <= applications.Length; i++)
                {
                    Assert.AreNotEqual(applications, string.Empty);
                }


                CollectionAssert.AllItemsAreNotNull(applications);
                CollectionAssert.AllItemsAreUnique(applications);

                client.Close();
            }

            catch (System.Exception ex)
            {
                client.Abort();
                Assert.Fail(ex.ToString());
            }
        }

        [TestMethod]
        [Description("Test if service get all Applications")]
        public void GetAllApplications()
        {
            var client = new AuthorizationServiceClient(ENDPOINTNAME);

            try
            {
                ApplicationCollection apps = client.GetAllApplications();

                CollectionAssert.AllItemsAreNotNull(apps);

                Assert.IsTrue(apps.Count > 0);
                CollectionAssert.AllItemsAreUnique(apps);
                CollectionAssert.AllItemsAreNotNull(apps); 

                client.Close();
            }

            catch (System.Exception ex)
            {
                client.Abort();
                Assert.Fail(ex.ToString());
            }
        }

        [TestMethod]
        [Description("Test if service gets a specified application")]
        public void GetApplication()
        {
            var client = new AuthorizationServiceClient(ENDPOINTNAME);

            try
            {
                Application app = client.GetApplication(APPLICATIONNAME1);

                Assert.IsNotNull(app);

                Assert.IsTrue(app.Name == APPLICATIONNAME1);

                client.Close();
            }
            catch (System.Exception ex)
            {
                client.Abort();

                Assert.Fail(ex.ToString());
            }

        }

        [TestMethod]
        [Description("Test if a application exists")]
        public void ApplicationExists()
        {
            var client = new AuthorizationServiceClient(ENDPOINTNAME);

            try
            {

                Assert.IsTrue(client.ApplicationExists(APPLICATIONNAME1));

                Assert.IsFalse(client.ApplicationExists("APPTHATNOTEXISTS"));
                                
                client.Close();
            }
            catch (System.Exception ex)
            {
                client.Abort();

                Assert.Fail(ex.ToString());
            }

        }

        
        #endregion

        #region Tests for Scopes

        [TestMethod]
        [Description("Test if service get all Scopes")]
        public void GetAllScopes()
        {
            var client = new AuthorizationServiceClient(ENDPOINTNAME);

            try
            {
                if (!client.ScopeExists(APPLICATIONNAME1, SCOPEAPP1_1))
                    client.CreateScope(APPLICATIONNAME1, SCOPEAPP1_1, null);

                Scope[] scopes = client.GetAllScopes(APPLICATIONNAME1);

                CollectionAssert.AllItemsAreNotNull(scopes);

                Assert.IsTrue(scopes.Length > 0);

                client.DeleteScope(APPLICATIONNAME1, SCOPEAPP1_1);
                client.Close();
            }

            catch (System.Exception ex)
            {
                client.Abort();
                Assert.Fail(ex.ToString());
            }
        }

        [TestMethod]
        [Description("Test if service gets a specified scope")]
        public void GetScope()
        {
            var client = new AuthorizationServiceClient(ENDPOINTNAME);

            try
            {
                if (!client.ScopeExists(APPLICATIONNAME1, SCOPEAPP1_1))
                    client.CreateScope(APPLICATIONNAME1, SCOPEAPP1_1, null);

                Scope scope = client.GetScope(APPLICATIONNAME1, SCOPEAPP1_1);

                Assert.IsNotNull(scope);

                Assert.IsTrue(scope.Name == SCOPEAPP1_1);

                client.DeleteScope(APPLICATIONNAME1, SCOPEAPP1_1);

                client.Close();
            }
            catch (System.Exception ex)
            {
                client.Abort();

                Assert.Fail(ex.ToString());
            }

        }

        [TestMethod]
        [Description("Test if service gets all Scope Names for a application specified")]
        public void GetAllScopeNames()
        {
            var client = new AuthorizationServiceClient(ENDPOINTNAME);

            try
            {
                string[] scopes = client.GetAllScopeNames(APPLICATIONNAME1);

                CollectionAssert.AllItemsAreNotNull(scopes);

                Assert.IsFalse(scopes.Length == 0);

                for (int i = 0; i <= scopes.Length; i++)
                {
                    Assert.AreNotEqual(scopes, string.Empty);
                }

                client.Close();
            }
            catch (System.Exception ex)
            {
                client.Abort();

                Assert.Fail(ex.ToString());
            }

        }

        [TestMethod]
        [Description("Test if a scope exists")]
        public void ScopeExists()
        {
            var client = new AuthorizationServiceClient(ENDPOINTNAME);

            try
            {
                if (!client.ScopeExists(APPLICATIONNAME1, SCOPEAPP1_1))
                    client.CreateScope(APPLICATIONNAME1, SCOPEAPP1_1, null);

                Assert.IsTrue(client.ScopeExists(APPLICATIONNAME1, SCOPEAPP1_1));

                Assert.IsFalse(client.ScopeExists(APPLICATIONNAME1, "SCOPETHATNOTEXISTS"));

                client.DeleteScope(APPLICATIONNAME1, SCOPEAPP1_1);

                client.Close();
            }
            catch (System.Exception ex)
            {
                client.Abort();

                Assert.Fail(ex.ToString());
            }

        }

        [TestMethod]
        [Description("Test if service creates specified Scope")]
        public void CreateScope()
        {
            var client = new AuthorizationServiceClient(ENDPOINTNAME);

            try
            {
                Scope tempscope = new Scope();

                tempscope.Name = "TESTSCOPE";

                tempscope.Description = "SCOPE TEST";

                if (client.ScopeExists(APPLICATIONNAME1, tempscope.Name))
                    client.DeleteScope(APPLICATIONNAME1, tempscope.Name);
            
                Scope scope = client.CreateScope(APPLICATIONNAME1, tempscope.Name, tempscope.Description);

                Assert.IsTrue(tempscope.Description == tempscope.Description);

                Assert.IsTrue(tempscope.Name == tempscope.Name);

                client.DeleteScope(APPLICATIONNAME1, tempscope.Name);

                client.Close();
            }
            catch (System.Exception ex)
            {
                client.Abort();

                Assert.Fail(ex.ToString());
            }

        }

        [TestMethod]
        [Description("Test if service deletes a specified scope")]
        public void DeleteScope()
        {
            var client = new AuthorizationServiceClient(ENDPOINTNAME);

            try
            {
                Scope temp = new Scope();

                temp.Description = "TESTSCOPEDESC";

                temp.Name = "TESTSCOPENAME";

                if (!client.ScopeExists(APPLICATIONNAME1, temp.Name))
                     client.CreateScope(APPLICATIONNAME1, temp.Name, temp.Description);

                client.DeleteScope(APPLICATIONNAME1, temp.Name);

                Assert.IsFalse(client.ScopeExists(APPLICATIONNAME1, temp.Name));

                client.Close();
            }
            catch (System.Exception ex)
            {
                client.Abort();

                Assert.Fail(ex.ToString());
            }

        }

        [TestMethod]
        [Description("Test the service that updates an existing scope")]
        public void UpdateScope()
        {
            var client = new AuthorizationServiceClient(ENDPOINTNAME);

            try
            {

                var upscopename = "UPDATEDSCOPENAME";

                var upscopedesc = "UPDATEDSCOPEDESC";

                var tempscope = new Scope {Name = "TESTSCOPE", Description = "DESCRIPTION SCOPE TEST"};

                if (client.ScopeExists(APPLICATIONNAME1, tempscope.Name))
                    client.DeleteScope(APPLICATIONNAME1, tempscope.Name);

                if (client.ScopeExists(APPLICATIONNAME1, upscopename))
                    client.DeleteScope(APPLICATIONNAME1, upscopename);


                client.CreateScope(APPLICATIONNAME1, tempscope.Name, tempscope.Description);

                client.UpdateScope(APPLICATIONNAME1, tempscope.Name, upscopename, upscopedesc);

                Assert.IsFalse(client.ScopeExists(APPLICATIONNAME1, tempscope.Name));

                Scope scope = client.GetScope(APPLICATIONNAME1, upscopename);

                Assert.IsTrue(scope.Description == upscopedesc);

                Assert.IsTrue(scope.Name == upscopename);
                
                client.Close();
            }
            catch (System.Exception ex)
            {
                client.Abort();

                Assert.Fail(ex.ToString());
            }

        }


        #endregion

        #region Tests for Utilities
            [TestMethod]
            [Description("Update Service Cache")]
            public void UpdateServiceCache()
            {
                var client = new AuthorizationServiceClient(ENDPOINTNAME);

                try
                {

                    client.UpdateServiceCache();
                    
                }
                catch (System.Exception ex)
                {
                    client.Abort();

                    Assert.Fail(ex.ToString());
                }

            }

        #endregion

        #region Userchecks

        [TestMethod]
        [Description("Get all Operations for a user")]
        public void GetOperationsForUser()
        {
            var client = new AuthorizationServiceClient(ENDPOINTNAME);

            try
            {
                Operation[] operations = client.GetOperationsForUser(USERNAME1, APPLICATIONNAME1, null);

                CollectionAssert.AllItemsAreNotNull(operations);

                //Assert.IsTrue(operations.Length > 0); // Delete, Read and Save
                if (client.ScopeExists(APPLICATIONNAME1, SCOPEAPP1_1))
                {
                    Operation[] operations2 = client.GetOperationsForUser(USERNAME1, APPLICATIONNAME1, SCOPEAPP1_1);

                    CollectionAssert.AllItemsAreNotNull(operations2);

                    //Assert.IsTrue(operations2.Length > 0);  // Delete and Read                             
                }
                client.Close();

            }
            catch (System.Exception ex)
            {
                client.Abort();

                Assert.Fail(ex.ToString());
            }

        }

        [TestMethod]
        [Description("Get All tasks for User")]
        public void GetTasksForUser()
        {
            var client = new AuthorizationServiceClient(ENDPOINTNAME);

            try
            {

                Task[] tasks = client.GetTasksForUser(USERNAME1, APPLICATIONNAME1, null);

                CollectionAssert.AllItemsAreNotNull(tasks);

                Assert.IsTrue(tasks.Length > 0);

                if (client.ScopeExists(APPLICATIONNAME1, SCOPEAPP1_1))
                {

                    Task[] tasks2 = client.GetTasksForUser(USERNAME1, APPLICATIONNAME1, SCOPEAPP1_1);
                    CollectionAssert.AllItemsAreNotNull(tasks2);

                    Assert.IsTrue(tasks2.Length > 0);
                }
                client.Close();

            }
            catch (System.Exception ex)
            {
                client.Abort();

                Assert.Fail(ex.ToString());
            }

        }

        [TestMethod]
        [Description("Get All RoleAssignments for User")]
        public void GetRoleAssignmentsForUser()
        {
            var client = new AuthorizationServiceClient(ENDPOINTNAME);

            try
            {

                var roles = client.GetRoleAssignmentsForUser(USERNAME1, APPLICATIONNAME1, null);

                CollectionAssert.AllItemsAreNotNull(roles);

                Assert.IsTrue(roles.Length > 0);  // Administrator

                if (client.ScopeExists(APPLICATIONNAME1, SCOPEAPP1_1))
                {
                    var roles2 = client.GetRoleAssignmentsForUser(USERNAME1, APPLICATIONNAME1, SCOPEAPP1_1);

                    CollectionAssert.AllItemsAreNotNull(roles2);

                    Assert.IsTrue(roles2.Length == 1); // Contributor
                }
                client.Close();

            }
            catch (System.Exception ex)
            {
                client.Abort();

                Assert.Fail(ex.ToString());
            }

        }

        [TestMethod]
        [Description("Get All role names for User")]
        public void GetRoleAssignmentNamesForUser()
        {
            var client = new AuthorizationServiceClient(ENDPOINTNAME);

            try
            {

                string[] roles = client.GetRoleAssignmentsNamesForUser(USERNAME1, APPLICATIONNAME1, null);

                CollectionAssert.AllItemsAreNotNull(roles);

                Assert.IsFalse(roles.Length == 0);

                for (int i = 0; i <= roles.Length; i++)
                {
                    Assert.AreNotEqual(roles, string.Empty);
                }

                client.Close();

            }
            catch (System.Exception ex)
            {
                client.Abort();

                Assert.Fail(ex.ToString());
            }

        }

        [TestMethod]
        [Description("Get All task names for User")]
        public void GetTaskNamesForUser()
        {
            var client = new AuthorizationServiceClient(ENDPOINTNAME);

            try
            {

                string[] tasks = client.GetTaskNamesForUser(USERNAME1, APPLICATIONNAME1, null);

                CollectionAssert.AllItemsAreNotNull(tasks);

                Assert.IsFalse(tasks.Length == 0);

                for (int i = 0; i <= tasks.Length; i++)
                {
                    Assert.AreNotEqual(tasks, string.Empty);
                }

                client.Close();

            }
            catch (System.Exception ex)
            {
                client.Abort();

                Assert.Fail(ex.ToString());
            }

        }

        [TestMethod]
        [Description("Check if a user has a specified role")]
        public void IsUserInRoleAssignment()
        {
            var client = new AuthorizationServiceClient(ENDPOINTNAME);

            try
            {

                bool userhasarole = client.IsUserInRoleAssignment(USERNAME1, APPLICATIONNAME1, null, ROLE_MANAGER);

                bool userhasarole2 = client.IsUserInRoleAssignment(USERNAME1, APPLICATIONNAME1, null, ROLE_READER);
           
                Assert.IsTrue(userhasarole);

                Assert.IsFalse(userhasarole2);

           
                client.Close();

            }
            catch (System.Exception ex)
            {
                client.Abort();

                Assert.Fail(ex.ToString());
            }

        }

        [TestMethod]
        [Description("Check if a user has a specified task")]
        public void IsUserInTask()
        {
            var client = new AuthorizationServiceClient(ENDPOINTNAME);

            try
            {

                bool userhasatask = client.IsUserInTask(USERNAME1, APPLICATIONNAME1, MANAGE_TASK, null);

                bool userhasatask2 = client.IsUserInTask(USERNAME1, APPLICATIONNAME1, READONLY_TASK, null);

                //bool userhasatask3 = client.IsUserInTask(USERNAME1, APPLICATIONNAME1, CONTRIBUTE_TASK, SCOPEAPP1_1);

                Assert.IsTrue(userhasatask);

                Assert.IsFalse(userhasatask2);

                //Assert.IsTrue(userhasatask3);

                client.Close();

            }
            catch (System.Exception ex)
            {
                client.Abort();

                Assert.Fail(ex.ToString());
            }

        }


        #region AcessCheck

        [TestMethod]
        [Description("Check a user access")]
        public void AccessCheck()
        {
            var client = new AuthorizationServiceClient(ENDPOINTNAME);

            try
            {

                bool userhaspermission = client.AccessCheck(USERNAME1, APPLICATIONNAME1, 1, null);

                bool userhaspermission2 = client.AccessCheck(USERNAME1, APPLICATIONNAME1, 2, null);

                bool userhaspermission3 = client.AccessCheck(USERNAME1, APPLICATIONNAME1, 3, null);
                
                //bool userhaspermission4 = client.AccessCheck("USERTHATNOTEXISTS", APPLICATIONNAME1, 4, null);

                Assert.IsTrue(userhaspermission);

                Assert.IsTrue(userhaspermission2);

                Assert.IsTrue(userhaspermission3);

                client.Close();

            }
            catch (System.Exception ex)
            {
                client.Abort();

                Assert.Fail(ex.ToString());
            }

        }

        [TestMethod]
        [Description("Check a user access by name")]
        public void AccessCheckByName()
        {
            var client = new AuthorizationServiceClient(ENDPOINTNAME);

            try
            {

                bool userhaspermission = client.AccessCheckByName(USERNAME1, APPLICATIONNAME1, OPERATION_READ, null);
                
                Assert.IsTrue(userhaspermission);

                client.Close();

            }
            catch (System.Exception ex)
            {
                client.Abort();

                Assert.Fail(ex.ToString());
            }

        }

        [TestMethod]
        [Description("Check a user access by name")]
        public void BulkAccessCheckByName()
        {
            var client = new AuthorizationServiceClient(ENDPOINTNAME);

            try
            {

                string[] operationnames = {OPERATION_READ, OPERATION_SAVE, OPERATION_DELETE };
                string[] scopenames = { SCOPEAPP1_1 };

                Operation[] result = client.BulkAccessCheckByName(USERNAME1, APPLICATIONNAME1, operationnames, null);

              //  Operation[] result2 = client.BulkAccessCheckByName(USERNAME1, APPLICATIONNAME1, operationnames, scopenames);

               // Operation[] result3 = client.BulkAccessCheckByName(USERNAME2, APPLICATIONNAME1, operationnames, scopenames);

                Assert.IsTrue(result.Length == 3);

                //Assert.IsTrue(result3.Length == 0);

                client.Close();

            }
            catch (System.Exception ex)
            {
                client.Abort();

                Assert.Fail(ex.ToString());
            }

        }

        [TestMethod]
        [Description("Check a user access by name")]
        public void BulkAccessCheck()
        {
            var client = new AuthorizationServiceClient(ENDPOINTNAME);

            try
            {

                int[] operationidss = { 1, 2, 3 };
                string[] scopenames = { SCOPEAPP1_1 };

                int[] result = client.BulkAccessCheck(USERNAME1, APPLICATIONNAME1, operationidss, null);                

                //int[] result3 = client.BulkAccessCheck(USERNAME2, APPLICATIONNAME1, operationidss, scopenames);

                Assert.IsTrue(result.Length == 3);

                //Assert.IsTrue(result3.Length == 2);

                client.Close();

            }
            catch (System.Exception ex)
            {
                client.Abort();

                Assert.Fail(ex.ToString());
            }

        }

        

        #endregion

            #endregion

    }
    }

